const gulp = require("gulp");
const cssmin = require("gulp-cssmin");
const autoprefixer = require("gulp-autoprefixer");
const uglify = require("gulp-uglify");
const babel = require("gulp-babel");
const htmlmin = require("gulp-htmlmin");
const webserver = require("gulp-webserver");
const clean = require("gulp-clean");
const sass = require("gulp-sass");
// 测试指令
// function testFn(){
//     console.log("这是我的第一个gulp指令");
// }
// module.exports.test = testFn;

// static的批量转存
function copyFileFn(){
    return gulp.src("./src/static/**/*")
            .pipe(gulp.dest("./dest/static"))
}
module.exports.copyFile = copyFileFn;

function watchFn(){
    return gulp.watch("./src/static/**/*",copyFileFn);
}
module.exports.watch = watchFn;
// 矢量图标文件夹的批量转存
function copyFontFn(){
    return gulp.src("./src/font_szmit39jz5/**/*")
        .pipe(gulp.dest("./dest/font_szmit39jz5"))
}
module.exports.copyFont = copyFontFn;
// 处理css的指令：压缩，添加兼容前缀
function cssFn(){
    return gulp.src("./src/css/**/*")
            .pipe(autoprefixer("last 2 version","safari 5","ie 8","ie 9","opera 12.1","ios 6","android 4"))
            .pipe(cssmin())
            .pipe(gulp.dest("./dest/css"))
}
module.exports.css = cssFn;

function watchCssFn(){
    return gulp.watch("./src/css/**/*",cssFn);
}
module.exports.watchCss = watchCssFn;

// 处理js的指令：ES6编译ES5，压缩
function jsFn(){
    return gulp.src("./src/js/**/*")
        .pipe(babel({
            presets:["@babel/env"]
        }))
        .pipe(uglify())
        .pipe(gulp.dest("./dest/js"))
}
module.exports.js = jsFn;

// 处理html的指令：压缩
function htmlFn(){
    return gulp.src("./src/pages/**/*")
        .pipe(htmlmin({
            removeEmptyAttributes:true,
            collapseWhitespace:true
        }))
        .pipe(gulp.dest("./dest/pages"))
}
module.exports.html = htmlFn;

// 处理sass文件，编译为css文件
function sassFn(){
    return gulp.src("./src/sass/**/*")
        .pipe(sass())
        .pipe(gulp.dest("./dest/css"))
}
module.exports.sass = sassFn;

// 删除文件
function cleanFn(){
    return gulp.src("./dest")
        .pipe(clean())
}
module.exports.clean = cleanFn;

// 服务器功能
function serverFn(){
    return gulp.src("./dest")
        .pipe(webserver({
            host:"localhost",
            port:"84",
            livereload:true,
            open:"./index.html"
        }))
}
module.exports.server = serverFn;

// 转存首页功能
function indexFn(){
    return gulp.src("./src/index.html")
        .pipe(gulp.dest("./dest/"))
}
module.exports.index = indexFn;

// 暴露批量执行指令
module.exports.most = gulp.parallel(htmlFn,jsFn,cssFn,copyFileFn,indexFn,copyFontFn);

// 监听所有文件
function watchAllFn(end){
    gulp.watch("./src/index.html",indexFn);
    gulp.watch("./src/css/**/*",cssFn);
    gulp.watch("./src/js/**/*",jsFn);
    gulp.watch("./src/pages/**/*",htmlFn);
    gulp.watch("./src/static/**/*",copyFileFn);
    gulp.watch("./src/sass/**/*",sassFn);
    gulp.watch("./src/font_szmit39jz5/**/*");
    end();
}
module.exports.watchAll = watchAllFn;

module.exports.all = gulp.series(gulp.parallel(htmlFn,jsFn,cssFn,copyFileFn,indexFn,copyFontFn),gulp.parallel(watchAllFn,serverFn));